<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Cgift extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		$view->assign('yes2no', $dlang->get('sh0007'));
		$view->assign('dict', $dlang->get('gf_dict'));
		$view->assign('type', $dlang->get('gf_type'));
		$view->assign('status', $dlang->get('gf_status'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/gift.ext.js');
	}
	
	/**
	 * @desc    保存礼物基础资料信息
	 * @doc     编辑/添加礼物
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$data['dictid'] = $filter->reset('dictid')->numeric()->min(1)->run();
			$data['type']   = $filter->reset('type')->numeric()->min(1)->run();
			$data['rank']   = $filter->reset('rank')->numeric()->min(0)->run();
			$data['reglv']  = $filter->reset('reglv')->numeric()->min(0)->run();
			$data['isshow'] = $filter->reset('isshow')->numeric()->min(0)->run();
			$data['isnew']  = $filter->reset('isnew')->numeric()->min(0)->run();
			$data['islucky']= $filter->reset('islucky')->numeric()->min(0)->run();
			$data['times']  = $filter->reset('times')->numeric()->min(0)->run();
			$data['limit']  = $filter->reset('limit')->numeric()->min(0)->run();
			$data['status'] = $filter->reset('status')->numeric()->min(0)->run();
			$data['isguard']= $filter->reset('isguard')->numeric()->min(0)->run();
			$data['name']   = $filter->reset('name')->required()->lbetween(1, 255)->run();
			$data['desc']   = $filter->reset('desc')->required()->lbetween(1, 255)->run();
			$data['coinprice'] = $filter->reset('coinprice')->numeric()->min(0)->run();
			$data['beanprice'] = $filter->reset('beanprice')->numeric()->min(0)->run();
			$giftids = auditor::intstr2arr('giftids');
			$data['isguard']= implode(',', $giftids);
			$acl  = \classlib\acl::inst();
			$res  = \src\logic\upfile::inst($acl)->post('Fileicon');
			if ($res) {
				$data['iconsrc'] = $res['url'];
			}
			$res  = \src\logic\upfile::inst($acl)->post('Fileswf');
			if ($res) {
				$data['swfsrc'] = $res['url'];
			}
			$res  = \src\logic\upfile::inst($acl)->post('Fileapp');
			if ($res) {
				$data['appsrc'] = $res['url'];
			}
			$res  = \src\logic\upfile::inst($acl)->post('Filegif');
			if ($res) {
				$data['gifsrc'] = $res['url'];
			}
			$res  = \src\logic\upfile::inst($acl)->post('Filebg');
			if ($res) {
				$data['bgsrc']  = $res['url'];
			}
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$id    = (int)input::get_post('id');
		$orm   = C\model::load('pro.inc.gift');
		$data['stime'] = C\config::$nowtime;
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    删除礼物配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang   = C\lang::inst('adm');
		$ids    = auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.inc.gift');
		$lorm   = C\model::load('pro.log.moneyflow');
		$where  = array(array('reftype', array(1, 8), \classlib\orm::OP_IN), 'refid'=>0);
		foreach ($ids as $id) {
			$where['refid'] = $id;
			$total  = $lorm->get_total($where, 'sum(nums)');
			if ($total > 0) {
				$view->assign_error(40008, $lang->common('40008'));
			}
			$data = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(C\view::inst('xml')->array2xml($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取用户资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$where  = array();
		list($start, $limit, $sort) = Cadmin::offset();
		$dictid = (int)input::get_post('dictid');
		if ($dictid > 0) {
			$where['dictid'] = $dictid;
		}
		$status = (int)input::get_post('status');
		if ($status > 0) {
			$where['status'] = $status;
		}
		$orm    = C\model::load('pro.inc.gift');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$lorm = C\model::load('log.log.moneyflow');
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$where= array(array('reftype', array(1, 8), \classlib\orm::OP_IN), 'refid'=>0);
			foreach ($list as &$item) {
				$where['refid'] = $item['id'];
				$item['count']  = $lorm->get_total($where, 'sum(nums)');
				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}